package com.njit.farm.controller;

import com.njit.farm.model.Result;
import com.njit.farm.service.UserService;
import com.njit.farm.util.IDUtils;
import com.njit.farm.util.SmsUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Time;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/api/user/")
public class UserController {

    @Resource
    UserService userService;


    @RequestMapping(value = "sendCheckCode",method = RequestMethod.POST)
    public Result sendCode(@RequestBody Map<String, String> params, HttpServletRequest request) {

        HttpSession session = request.getSession();

        String code = IDUtils.checkCode();
        String phone = params.get("phone");

        SmsUtils.sendSms(phone, code);

        session.setAttribute("checkCode", code);
        System.out.println(code);
        //5分钟失效
        ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
                new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
        executorService.schedule(new Runnable() {
            @Override
            public void run() {
                session.removeAttribute("checkCode");
            }
        }, 5, TimeUnit.MINUTES);



        Result result = new Result();
        result.setSucc(true);
        return result;
    }

    @RequestMapping(value = "test",method = RequestMethod.GET)
    public Result test(HttpServletRequest request){
        HttpSession session = request.getSession();
        String phone = (String) session.getAttribute("checkCode");
        Result result = new Result();
        result.setSucc(true);
        result.setMsg(phone);

        return result;
    }


    @RequestMapping(value = "register",method = RequestMethod.POST)
    public Result reg(@RequestBody Map<String, String> params, HttpServletRequest request) {

        Result result = new Result();

        String phone = params.get("phone");
        String code = params.get("code");
        String pwd = params.get("pwd");

        HttpSession session = request.getSession();
        String checkCode = (String) session.getAttribute("checkCode");
        if (checkCode == null) {
            result.setSucc(false);
            result.setMsg("验证码不正确");
            return result;
        }
        if (StringUtils.equals(code, checkCode)) {
            return userService.register(phone, pwd);
        }

        return result;
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    public Result login(@RequestBody Map<String,String> params){


        String phone = params.get("phone");
        String pwd = params.get("pwd");

        return userService.login(phone, pwd);
    }
}
